import express from 'express';
import { checkSchema, validationResult } from 'express-validator'  
import util from 'utility'
import jwt from 'jsonwebtoken';  

const router = express.Router();


// 新增的操作
router.post('/patient/add',checkSchema({
    realName: { notEmpty: true, errorMessage:'真实姓名不能为空'},
    docNo: { notEmpty: true, errorMessage:'病历号不能为空'},
    sex: { notEmpty: true, errorMessage:'性别不能为空' }
}), async (req,res)=>{
    res.validator();  // 对于请求参数进行校验

    const {realName, docNo,sex,age,idNo ,birth,createdAt,avatar} = req.body;

    const data = {
        realName,
        docNo,
        sex
    }
    if(sex) data.sex = sex;
    if(age) data.age = Number(age);
    if(idNo) data.idNo = idNo;
    if(birth) data.birth = new Date(birth);
    if(createdAt) data.createdAt = new Date(createdAt);
    if(avatar) data.avatar = avatar
    

    await req.prisma.patient.create({
        data
    })
    res.success('患者添加成功')

})

// 删除患者
router.post('/patient/del', checkSchema({
    id: { notEmpty: true, errorMessage: '患者id不能为空'}
}),async (req,res)=>{
    res.validator();  // 对于请求参数进行校验

    const { id } = req.body;
    await req.prisma.patient.delete({
        where:{
            id: Number(id)
        }
    })
    res.success('删除成功')
})

// 修改患者信息
router.post('/patient/edit',checkSchema({
    id: { notEmpty: true, errorMessage: '患者id不能为空'}
}), async (req,res)=>{
    res.validator();  // 对于请求参数进行校验

    const {realName, docNo,sex,age,idNo ,birth,createdAt,avatar ,id } = req.body;

    const data = { }
    if(realName) data.realName = realName;
    if(docNo) data.docNo  = docNo;
    if(sex) data.sex = sex;
    if(age) data.age = Number(age);
    if(idNo) data.idNo = idNo;
    if(birth) data.birth = new Date(birth);
    if(createdAt) data.createdAt = new Date(createdAt);
    if(avatar) data.avatar = avatar
    

    await req.prisma.patient.update({
        where:{
            id
        },
        data
    })
    res.success('患者修改')

})


router.get('/patient/info', checkSchema({
    // docNo: { notEmpty: true, errorMessage: '请传入一个正确的病历号' },
}), async (req,res)=>{
    const { docNo,realName } = req.query;
    const where = { OR: []}
    if(docNo) {
        where.OR.push( { docNo: { contains: docNo } } )
    }
    if(realName) {
        where.OR.push( { realName: { contains: realName } } )
    }
    const patient = await  req.prisma.patient.findFirst({
        where,
        include:{
            user: true, // 联表查询对应的用户信息
        },
      
    })
    res.success(patient)
})

router.get('/patient/list', async(req,res)=>{

    let { limit = 30, page =1,keyword  } = req.query;
    limit = Number(limit)
    page = Number(page)

    const where = {};
    if(keyword){
        where.OR = [
            { 
                docNo: keyword
             },
             {
                realName: keyword
             }
        ]
    }

    const list = await req.prisma.patient.findMany({
        where,
        skip: (page-1)* limit,
        take: limit, // 一次查询的数量
        orderBy: {
            id:'desc'
        }
    })  

    const count = await req.prisma.patient.count(); // 统计总数

    res.success({ list, count })
    
})


// 获取当天最大的一个用户

export default router;